home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection Student Program / ADC Tools Sampler CD Disk 3 1999.iso / Metrowerks CodeWarrior / Java Support / Java_Source / Java2 / src / javax / swing / JRadioButton.java < prev    next >
Encoding:
Java Source  |  1999-05-28  |  7.5 KB  |  250 lines  |  [TEXT/CWIE]

  1. /*
  2.  * @(#)JRadioButton.java    1.51 98/08/28
  3.  *
  4.  * Copyright 1997, 1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14. package javax.swing;
  15.  
  16. import java.awt.*;
  17. import java.awt.event.*;
  18.  
  19. import javax.swing.plaf.*;
  20. import javax.accessibility.*;
  21.  
  22. import java.io.ObjectOutputStream;
  23. import java.io.ObjectInputStream;
  24. import java.io.IOException;
  25.  
  26.  
  27. /**
  28.  * An implementation of a radio button -- an item that can be selected or
  29.  * deselected, and which displays its state to the user.
  30.  * Used with a {@link ButtonGroup} object to create a group of buttons
  31.  * in which only one button at a time can be selected. (Create a ButtonGroup
  32.  * object and use its <code>add</code> method to include the JRadioButton objects
  33.  * in the group.)
  34.  * <blockquote>
  35.  * Note:<br>
  36.  * The ButtonGroup object is a logical grouping -- not a physical grouping.
  37.  * Tocreate a button panel, you should still create a {@link JPanel} or similar
  38.  * container-object and add a {@link Border} to it to set it off from surrounding
  39.  * components.
  40.  * <blockquote>
  41.  * <p>
  42.  * See <a href="http://java.sun.com/docs/books/tutorial/ui/swing/radiobutton.html">How to Use Radio Buttons</a>
  43.  * in <a href="http://java.sun.com/Series/Tutorial/index.html"><em>The Java Tutorial</em></a>
  44.  * for further documentation.
  45.  * <p>
  46.  * For the keyboard keys used by this component in the standard Look and
  47.  * Feel (L&F) renditions, see the
  48.  * <a href="doc-files/Key-Index.html#JRadioButton">JRadioButton</a> key assignments.
  49.  * <p>
  50.  * <strong>Warning:</strong>
  51.  * Serialized objects of this class will not be compatible with
  52.  * future Swing releases.  The current serialization support is appropriate
  53.  * for short term storage or RMI between applications running the same
  54.  * version of Swing.  A future release of Swing will provide support for
  55.  * long term persistence.
  56.  * 
  57.  * @beaninfo
  58.  *   attribute: isContainer false
  59.  * @see ButtonGroup
  60.  * @see JCheckBox
  61.  * @version 1.51 08/28/98
  62.  * @author Jeff Dinkins
  63.  */
  64. public class JRadioButton extends JToggleButton implements Accessible {
  65.  
  66.     /**
  67.      * @see #getUIClassID
  68.      * @see #readObject
  69.      */
  70.     private static final String uiClassID = "RadioButtonUI";
  71.  
  72.  
  73.     /**
  74.      * Creates an initially unselected radio button
  75.      * with no set text.
  76.      */
  77.     public JRadioButton () {
  78.         this(null, null, false);
  79.     }
  80.      
  81.     /**
  82.      * Creates an initially unselected radio button
  83.      * with the specified image but no text.
  84.      *
  85.      * @param icon  the image that the button should display
  86.      */
  87.     public JRadioButton(Icon icon) {
  88.         this(null, icon, false);
  89.     }
  90.  
  91.     /**
  92.      * Creates a radio button with the specified image
  93.      * and selection state, but no text.
  94.      *   
  95.      * @param icon  the image that the button should display
  96.      * @param selected  if true, the button is initially selected;
  97.      *                  otherwise, the button is initially unselected
  98.      */
  99.     public JRadioButton(Icon icon, boolean selected) {
  100.         this(null, icon, selected);
  101.     }
  102.     
  103.     /**
  104.      * Creates an unselected radio button with the specified text.
  105.      * 
  106.      * @param text  the string displayed on the radio button
  107.      */
  108.     public JRadioButton (String text) {
  109.         this(text, null, false);
  110.     }
  111.  
  112.     /**
  113.      * Creates a radio button with the specified text
  114.      * and selection state.
  115.      * 
  116.      * @param text  the string displayed on the radio button
  117.      * @param selected  if true, the button is initially selected;
  118.      *                  otherwise, the button is initially unselected
  119.      */
  120.     public JRadioButton (String text, boolean selected) {
  121.         this(text, null, selected);
  122.     }
  123.  
  124.     /**
  125.      * Creates a radio button that has the specified text and image,
  126.      * and that is initially unselected.
  127.      *
  128.      * @param text  the string displayed on the radio button 
  129.      * @param icon  the image that the button should display
  130.      */
  131.     public JRadioButton(String text, Icon icon) {
  132.         this(text, icon, false);
  133.     }
  134.  
  135.     /**
  136.      * Creates a radio button that has the specified text, image,
  137.      * and selection state.
  138.      *
  139.      * @param text  the string displayed on the radio button 
  140.      * @param icon  the image that the button should display
  141.      */
  142.     public JRadioButton (String text, Icon icon, boolean selected) {
  143.         super(text, icon, selected);
  144.         setBorderPainted(false);
  145.         setHorizontalAlignment(LEFT);
  146.     }
  147.  
  148.  
  149.     /**
  150.      * Notification from the UIFactory that the L&F
  151.      * has changed. 
  152.      *
  153.      * @see JComponent#updateUI
  154.      */
  155.     public void updateUI() {
  156.         setUI((ButtonUI)UIManager.getUI(this));
  157.     }
  158.     
  159.  
  160.     /**
  161.      * Returns the name of the L&F class
  162.      * that renders this component.
  163.      *
  164.      * @return String "RadioButtonUI"
  165.      * @see JComponent#getUIClassID
  166.      * @see UIDefaults#getUI
  167.      * @beaninfo
  168.      *        expert: true
  169.      *   description: A string that specifies the name of the L&F class.
  170.      */
  171.     public String getUIClassID() {
  172.         return uiClassID;
  173.     }
  174.  
  175.  
  176.     /** 
  177.      * See readObject() and writeObject() in JComponent for more 
  178.      * information about serialization in Swing.
  179.      */
  180.     private void writeObject(ObjectOutputStream s) throws IOException {
  181.         s.defaultWriteObject();
  182.     if ((ui != null) && (getUIClassID().equals(uiClassID))) {
  183.         ui.installUI(this);
  184.     }
  185.     }
  186.  
  187.  
  188.     /**
  189.      * Returns a string representation of this JRadioButton. This method 
  190.      * is intended to be used only for debugging purposes, and the 
  191.      * content and format of the returned string may vary between      
  192.      * implementations. The returned string may be empty but may not 
  193.      * be <code>null</code>.
  194.      * <P>
  195.      * Overriding paramString() to provide information about the
  196.      * specific new aspects of the JFC components.
  197.      * 
  198.      * @return  a string representation of this JRadioButton.
  199.      */
  200.     protected String paramString() {
  201.     return super.paramString();
  202.     }
  203.  
  204.  
  205. /////////////////
  206. // Accessibility support
  207. ////////////////
  208.  
  209.  
  210.     /**
  211.      * Get the AccessibleContext associated with this JComponent
  212.      *
  213.      * @return the AccessibleContext of this JComponent
  214.      * @beaninfo
  215.      *       expert: true
  216.      *  description: The AccessibleContext associated with this Button
  217.      */
  218.     public AccessibleContext getAccessibleContext() {
  219.         if (accessibleContext == null) {
  220.             accessibleContext = new AccessibleJRadioButton();
  221.         }
  222.         return accessibleContext;
  223.     }
  224.  
  225.     /**
  226.      * The class used to obtain the accessible role for this object.
  227.      * <p>
  228.      * <strong>Warning:</strong>
  229.      * Serialized objects of this class will not be compatible with
  230.      * future Swing releases.  The current serialization support is appropriate
  231.      * for short term storage or RMI between applications running the same
  232.      * version of Swing.  A future release of Swing will provide support for
  233.      * long term persistence.
  234.      */
  235.     protected class AccessibleJRadioButton extends AccessibleJToggleButton {
  236.  
  237.         /**
  238.          * Get the role of this object.
  239.          *
  240.          * @return an instance of AccessibleRole describing the role of the object
  241.          * @see AccessibleRole
  242.          */
  243.         public AccessibleRole getAccessibleRole() {
  244.             return AccessibleRole.RADIO_BUTTON;
  245.         }
  246.  
  247.     } // inner class AccessibleJRadioButton
  248. }
  249.   
  250.